package com.ruoyi.web.controller.gcgl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GcProjectSbclht;
import com.ruoyi.gcgl.domain.GcProjectSbclhtFj;
import com.ruoyi.gcgl.domain.GcProjectXmxq;
import com.ruoyi.gcgl.service.IGcProjectSbclhtFjService;
import com.ruoyi.gcgl.service.IGcProjectSbclhtService;
import com.ruoyi.gcgl.service.IGcProjectService;

/**
 * 设备材料合同附件单Controller
 * 
 * @author ruoyi
 * @date 2023-01-23
 */
@Controller
@RequestMapping("/gcgl/sbclhtFj")
public class GcProjectSbclhtFjController extends BaseController
{
    private String prefix = "gcgl/sbclhtFj";

    @Autowired
    private IGcProjectSbclhtFjService gcProjectSbclhtFjService;
    @Autowired
    private IGcProjectSbclhtService gcProjectSbclhtService;
    @Autowired
    private IGcProjectService gcProjectService;

    @RequiresPermissions("gcgl:sbclhtFj:view")
    @GetMapping()
    public String sbclhtFj()
    {
        return prefix + "/sbclhtFj";
    }

    /**
     * 查询设备材料合同附件单列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GcProjectSbclhtFj gcProjectSbclhtFj)
    {
        startPage();
        gcProjectSbclhtFj.setDeleteFlag(1);
        gcProjectSbclhtFj.setType("2");
        if(getSysUser().isShow()){
        gcProjectSbclhtFj.setCreateUser(getLoginName());
        }
        List<GcProjectSbclhtFj> list = gcProjectSbclhtFjService.selectGcProjectSbclhtFjList(gcProjectSbclhtFj);
        
        for(GcProjectSbclhtFj zjbj:list){
        	if(zjbj.getZilwj()!=null && !zjbj.getZilwj().equals("")){
        	zjbj.setZilwj(RuoYiConfig.getOnPath()+zjbj.getZilwj());
        	}
        }
        
        return getDataTable(list);
    }

    /**
     * 导出设备材料合同附件单列表
     */
    @Log(title = "设备材料合同附件单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcProjectSbclhtFj gcProjectSbclhtFj)
    {
        List<GcProjectSbclhtFj> list = gcProjectSbclhtFjService.selectGcProjectSbclhtFjList(gcProjectSbclhtFj);
        ExcelUtil<GcProjectSbclhtFj> util = new ExcelUtil<GcProjectSbclhtFj>(GcProjectSbclhtFj.class);
        return util.exportExcel(list, "设备材料合同附件单数据");
    }

    /**
     * 新增设备材料合同附件单
     */
    @GetMapping("/add/{sbclhtId}")
    public String add(ModelMap mmap,@PathVariable("sbclhtId") Long sbclhtId){
    	GcProjectSbclht gcProjectSbclht = gcProjectSbclhtService.selectGcProjectSbclhtById(sbclhtId);
    	mmap.put("gcProjectSbclht", gcProjectSbclht);
        return prefix + "/add";
    }

    /**
     * 新增保存设备材料合同附件单
     */
    @Log(title = "设备材料合同附件单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GcProjectSbclhtFj gcProjectSbclhtFj,@RequestParam("zilwjfile")MultipartFile filewj)
    {
    	try {
    		if (!filewj.isEmpty()){
                String zilwj = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), filewj, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
                gcProjectSbclhtFj.setZilwj(zilwj);
            }
    		gcProjectSbclhtFj.setType("2");
    		gcProjectSbclhtFj.setDeleteFlag(1);
    		gcProjectSbclhtFj.setCreateAt(new Date());
    		gcProjectSbclhtFj.setUpdateAt(new Date());
    		gcProjectSbclhtFj.setCreateUser(getLoginName());
    		gcProjectSbclhtFj.setUpdateUser(getLoginName());
    		gcProjectSbclhtFjService.insertGcProjectSbclhtFj(gcProjectSbclhtFj);
		} catch (Exception e) {
			return error(e.getMessage());
		}
        return toAjax(1);
    }

    /**
     * 修改设备材料合同附件单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcProjectSbclhtFj gcProjectSbclhtFj = gcProjectSbclhtFjService.selectGcProjectSbclhtFjById(id);
        if(gcProjectSbclhtFj.getZilwj()!=null && !gcProjectSbclhtFj.getZilwj().equals("")){
        gcProjectSbclhtFj.setZilwj(RuoYiConfig.getOnPath()+gcProjectSbclhtFj.getZilwj());
        }
        mmap.put("gcProjectSbclhtFj", gcProjectSbclhtFj);
        GcProjectSbclht gcProjectSbclht = 
        		gcProjectSbclhtService.selectGcProjectSbclhtById(gcProjectSbclhtFj.getSbclhtId());
        mmap.put("gcProjectSbclht", gcProjectSbclht);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备材料合同附件单
     */
    @Log(title = "设备材料合同附件单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcProjectSbclhtFj gcProjectSbclhtFj,@RequestParam("zilwjfile")MultipartFile filewj)
    {
    	try{
            if (!filewj.isEmpty()){
                String zilwj = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), filewj, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
                gcProjectSbclhtFj.setZilwj(zilwj);
            }
            gcProjectSbclhtFj.setUpdateAt(new Date());
    		gcProjectSbclhtFj.setUpdateUser(getLoginName());
            gcProjectSbclhtFjService.updateGcProjectSbclhtFj(gcProjectSbclhtFj);
        }
        catch (Exception e){
            return error(e.getMessage());
        }
    	
        return toAjax(1);
    }

    /**
     * 删除设备材料合同附件单
     */
    @RequiresPermissions("gcgl:sbclhtFj:delete")
    @Log(title = "设备材料合同附件单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcProjectSbclhtFjService.deleteGcProjectSbclhtFjByIds(ids));
    }
}